package com.ibiaoke.goods.service;

import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.goods.dao.GoodsUnitDao;
import com.ibiaoke.goods.dao.XcxShopPriceDao;
import com.ibiaoke.goods.entity.Adjust;
import com.ibiaoke.goods.entity.GoodsUnit;
import com.ibiaoke.goods.entity.ReportLoss;
import com.ibiaoke.goods.entity.XcxShopPrice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author Dragon
 * @date 2018/3/26/10:36;
 */
@Service
public class XcxShopPriceServiceImpl implements XcxShopPriceService {

    @Autowired
    private XcxShopPriceDao xcxShopPriceDao;

    @Autowired
    private GoodsUnitDao goodsUnitDao;

    @Override
    public Integer batchSaveShopPrice(List<XcxShopPrice> xcxShopPrices) {
        AssertUtils.notEmpty(xcxShopPrices, "参数不正确，请检查参数");
        xcxShopPrices.forEach(XcxShopPriceServiceImpl::validate);
        Integer xcxGoodsId = xcxShopPrices.get(0).getGoodsId();
        xcxShopPriceDao.delShopPrice(xcxGoodsId, null);
        for (XcxShopPrice shopPrices : xcxShopPrices) {
            shopPrices.setShopPriceId(null);
            //shopPrices.setGroupId(shopPrices.getSpec());
            xcxShopPriceDao.insertSelective(shopPrices);
        }
        return null;
    }

    @Override
    public Integer saveShopPrice(XcxShopPrice xcxShopPrices) {
        AssertUtils.notNull(xcxShopPrices, "参数不正确，请检查参数");
        validate(xcxShopPrices);
        xcxShopPriceDao.delShopPrice(xcxShopPrices.getGoodsId(), xcxShopPrices.getShopId());
        Integer count = xcxShopPriceDao.insertSelective(xcxShopPrices);
        return count;
    }

    @Override
    public List<XcxShopPrice> listShopPrice(Integer xcxGoodsId) {
        AssertUtils.notNull(xcxGoodsId, "商品id不能为空");
        List<XcxShopPrice> xcxShopPriceList = xcxShopPriceDao.listShopPrices(xcxGoodsId);

        /*List<GoodsUnit> unitList = goodsUnitDao.listGoodsUnit();

        xcxShopPriceList.forEach(xcxShopPrice -> unitList.forEach(goodsUnit -> {
            if (Objects.equals(goodsUnit.getUnitId(), xcxShopPrice.getUnitId())) {
                xcxShopPrice.setGoodsUnit(goodsUnit);
            }
        }));*/
        return xcxShopPriceList;
    }

    @Override
    public Double getGoodCount(Integer goodId) {
        List<XcxShopPrice> xcxShopPriceList = xcxShopPriceDao.listShopPrices(goodId);
        double count = 0;
        for (XcxShopPrice xcxShopPrice : xcxShopPriceList) {
            count = count + xcxShopPrice.getStockNum();
        }
        return count;
    }

    @Override
    public Integer modifyOutShopNum(Adjust adjust) {
        XcxShopPrice xcxShopPrice = xcxShopPriceDao.findGood(adjust.getOutShopId(), adjust.getGoodId());
        if (xcxShopPrice.getStockNum() < adjust.getAdjustNum()) {
            throw new ServiceException("库存不足，无法完成调货");
        }
        xcxShopPriceDao.updateXcxGoodStockNum(adjust.getGoodId(), adjust.getOutShopId(), adjust.getAdjustNum() * -1);
        xcxShopPriceDao.updateXcxGoodStockNum(adjust.getGoodId(), adjust.getInShopId(), adjust.getAdjustNum());
        return null;
    }

    @Override
    public Integer modifyReportLossShopNum(ReportLoss reportLoss) {
        XcxShopPrice xcxShopPrice = xcxShopPriceDao.findGood(reportLoss.getShopId(), reportLoss.getGoodId());
        if (xcxShopPrice.getStockNum() < reportLoss.getLossNum()) {
            throw new ServiceException("库存不足，无法完成报损");
        }
        xcxShopPriceDao.updateXcxGoodStockNum(reportLoss.getGoodId(), reportLoss.getShopId(), reportLoss.getLossNum() * -1);
        return null;
    }

    private static void validate(XcxShopPrice xcxShopPrice) {
        AssertUtils.notNull(xcxShopPrice.getShopId(), "店铺id不能为空");
        AssertUtils.notNull(xcxShopPrice.getGoodsId(), "商品id不能为空");
        AssertUtils.notNull(xcxShopPrice.getSpec(), "商品规格不能为空");
        AssertUtils.notNull(xcxShopPrice.getPrice(), "商品价格不能为空");
    }
}
